﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;
using LED_Utils.MultitrackControl;

namespace Maxel.MultitrackControl
{
    public class ZoomableScrollableFlowLayoutPanel : FlowLayoutPanel, ICustomScale 
	{

        public ZoomableScrollableFlowLayoutPanel()
        {
            this.SetStyle(ControlStyles.Selectable, true);
        }

		double scale = 100;
		double previousScale = 100;
		double delta = 1;

		float calcFactor = 1;


     
        public void PerformScroll(int offset)
        {
            Point p = AutoScrollPosition;
            AutoScrollPosition = new Point(Math.Abs(p.X) + offset, p.Y);
            //ShowInfo();


        }
        public void PerformScrollPage(int pageCount)
        {
            PerformScroll(pageCount * ClientRectangle .Width);
            //ShowInfo();

        }

		[DllImport("user32.dll")]
		static extern bool ShowScrollBar(IntPtr hWnd, int wBar, bool bShow);

		private const int SB_HORZ=0;

		protected override void OnSizeChanged(EventArgs e)
		{
			ShowScrollBar(Handle, SB_HORZ, false);
			base.OnSizeChanged(e);
		}

        #region ICustomScale Members

        public virtual void PerformScale(int percents)
        {
            previousScale = scale;
            scale = percents;

            delta = scale - previousScale;


            //int x = scale - previousScale > 0 ? 1 : -1;
            int x = 1;
            //calcFactor=(float)(scale / previousScale);
            calcFactor = 1f + (float)(x * delta / previousScale);
            SuspendLayout();
            foreach (Control item in Controls)
            {
                if (item is ICustomScale)
                {
                    ((ICustomScale)item).InitialScale = InitialScale;
                    ((ICustomScale)item).ScaleFactor = ScaleFactor;

                }
                item.Scale(new SizeF(calcFactor, 1f));
            }
            ResumeLayout();
            //ShowInfo();
        }


        private int _initialScale;
        public int InitialScale
        {
            get
            {
                return _initialScale;
            }
            set
            {
                _initialScale = value;
            }
        }

        #endregion

        #region ICustomScale Members


        public float ScaleFactor
        {
            get
            {
                return calcFactor;
            }
            set
            {
                //calcFactor = ;
            }
        }

        #endregion
    }
}
